package com.thinksoft.ui;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TableLayout;

import java.util.List;

/**
 * @author txf
 * @create 2020/3/25 0025
 * @
 */
public class ImmerseFragmentActivity extends AppCompatActivity {
    TabLayout tabLayout;
    String[] titles;
    Tab1Fragment mTab1Fragment;
    Tab2Fragment mTab2Fragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置状态栏颜色为透明
        UITools.setStatusBarColor(this, 0x00000000);
        setContentView(R.layout.activity_immerse_fragment);

        tabLayout = findViewById(R.id.tabLayout);
        titles = new String[]{"tab1", "tab2"};
        for (int i = 0; i < titles.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            tab.setTag(i);
            tab.setText(titles[i]);
            tabLayout.addTab(tab);
        }
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                showFragmentUI((int) tab.getTag());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        showFragmentUI(0);
    }

    private void showFragmentUI(int tag) {
        switch (tag) {
            case 0://tab1
                if (mTab1Fragment == null) {
                    mTab1Fragment = new Tab1Fragment();
                }
                UITools.setAndroidNativeLightStatusBar(this, false);
                showFragment(R.id.frameLayout, mTab1Fragment, Tab1Fragment.TAG);
                break;
            case 1://tab2
                if (mTab2Fragment == null) {
                    mTab2Fragment = new Tab2Fragment();
                }
                UITools.setAndroidNativeLightStatusBar(this, true);
                showFragment(R.id.frameLayout, mTab2Fragment, Tab2Fragment.TAG);
                break;
        }
    }

    private void showFragment(int containerViewId, Fragment fragment, String tag) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        List<Fragment> frags = manager.getFragments();
        for (Fragment frag : frags) {
            if (frag == null)
                break;
            transaction.hide(frag);
        }
        if (manager.findFragmentByTag(tag) == null) {
            transaction.add(containerViewId, fragment, tag);
        } else {
            transaction.show(manager.findFragmentByTag(tag));
        }
        transaction.commit();
    }
}
